using UnityEngine;
using System.Collections;



public class ZombieAI : MonoBehaviour {

	public float speedMin= 0.5f;
	public float speedMax= 0.5f;
	private float speed;

	public float atkCooldown = 3;

	public int atkDamageMin = 10;
	public int atkDamageMax = 10;
	private int atkDamage;

	public int healthMin = 100;
	public int healthMax = 100;
	public int health;

	public AudioClip[] atkSound;
	public AudioClip hurtSound;

	public Vector3 rayStart;
	public Vector3 rayDirection;
	public Ray wallCheck;
	
	private float atkTimer = 0;
	private GameObject player;
	private float distance;

	private int scoreValue;
	private float speedTemp;

	private Vector3 direction;

	private Vector3 moveDirection = Vector3.zero;

	Animator anim;




	// roter Ray zum debuggen
	void OnDrawGizmos()
	{
		Gizmos.color = Color.red;
		Gizmos.DrawRay(rayStart, transform.forward*100);	
	}
	


	// Use this for initialization
	void Start ()
	{
		// Eingabewerte prüfen und Startzufallswerte für den Zombie erstellen
		if (speedMax < speedMin)
			speedMax = speedMin;
		speed = Random.Range (speedMin, speedMax);
		speedTemp = speed;

		if (atkDamageMax < atkDamageMin)
			atkDamageMax = atkDamageMin;
		atkDamage = Random.Range (atkDamageMin, atkDamageMax);

		if (healthMax < healthMin)
			healthMax = healthMin;
		health = Random.Range (healthMin, healthMax);

		scoreValue = ((health/10) + atkDamage) * (int)(speed * 10);


		player= GameObject.FindWithTag("Player");


		//Ray erstellen
		rayStart = GameObject.Find("Tops").transform.position;
		rayDirection = transform.forward;
		anim = GetComponent<Animator>();
	}
	


	// Update is called once per frame
	void Update ()
	{
		rayStart = GameObject.Find("Tops").transform.position;
		rayDirection = transform.forward;
		wallCheck = new Ray(rayStart,transform.forward*100);



		//Zombie GameObjekt zerstören und Combo starten
		if(health <= 0)
		{
			audio.clip = atkSound[Random.Range(0,atkSound.Length)];
			audio.Play();

			Camera.main.GetComponent<ComboCounterScript>().comboTime = 3.0f;
			Camera.main.GetComponent<ComboCounterScript>().killCounter ++;
			Camera.main.GetComponent<ComboCounterScript>().scoreTemp += scoreValue;


			audio.clip = atkSound[Random.Range(0,atkSound.Length)];
			audio.Play();
			GameObject.Destroy(this.gameObject);

		}



		/*

		// Wenn an wand gelaufen dann eine zufällige andere Richtung einschlagen
		if (WallCollide())
		{
			transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, new Vector3(0f, transform.eulerAngles.y+15, 0f), 0.01f);
		}
		else
		{
			print ("else");

		}
		*/

		direction = -(transform.position - player.transform.position);

		transform.forward = Vector3.Slerp (transform.forward, direction, 0.05f);

		transform.position += transform.forward/25;

		float move = 0;
		anim.SetFloat("speed",move);

		/*CharacterController controller = GetComponent<CharacterController>();
		moveDirection = GameObject.FindGameObjectWithTag("Player").transform.position;
		moveDirection.Normalize();
		
		moveDirection *= speedTemp;
		controller.Move(moveDirection * Time.deltaTime);*/


		// BRAIN!!!!!!!    :D
		distance = Vector3.Distance (player.transform.position, transform.position);
			if (distance >= 10)
						speedTemp = speed;

			if (distance < 1.2)
				{
					speedTemp = 0;

						if (atkTimer > 0)
							atkTimer -= Time.deltaTime;

						if (atkTimer < 0)
							atkTimer = 0;

						if (atkTimer == 0)
						{	
							// Damage per Script auf Player anrechnen
							Camera.main.GetComponent<GameManagerScript>().playerHealth = Camera.main.GetComponent<GameManagerScript>().playerHealth - atkDamage;

							// stylisches Brüllen für MAXIMUM FEAR!
							audio.clip = atkSound[Random.Range(0,atkSound.Length)];
							audio.Play();
							AudioSource.PlayClipAtPoint( hurtSound, GameObject.FindGameObjectWithTag("Player").transform.position );

							// angriff wieder zurücksetzen auf eingestellten Cooldown
							atkTimer = atkCooldown;
						}
				}
	}



	bool WallCollide ()
	{
		RaycastHit hit;

		if (Physics.Raycast(wallCheck, out hit, 1))
		{
			//Vector3 treffpunkt = hit.point;
			Collider treffer = hit.collider;

			if(treffer.tag != "Player")
			{
				//print ("roam");
				return true;
			}
		}

		return false;
		
	}



}
